module.exports=class extends baseLogic{
    constructor(){
        super();
    }

    init(){
        return new this();
    }

    async getItems(queryModel){
        let ret = await this.DB.cms_site_menu.Access.findAll({
            order: [
                ['sm_sort', 'ASC']
            ],
            raw:true
        });
        return ret;
    }

    async insertOrUpdate(data){
        let Model = {};
        let sm_id = data.sm_id || '';
        let sm_parentId = data.sm_parentId!=="" ? data.sm_parentId : "0";
        if (sm_id  && sm_id!=='') {
            Model = await this.DB.cms_site_menu.Access.findById(sm_id);
            if (!Model) {
                return null;
            }
            Model = Model.dataValues;
        } else {
            var MaxSort = await this.DB.cms_site_menu.Access.max("sm_sort", {
                where: {
                    sm_parentId: sm_parentId
                }
            });
            if (isNaN(MaxSort)) {
                MaxSort = 0;
            }
            Model.sm_id=this.webTools.myuuid.createUUID();
            Model.sm_isParent = 0;
            Model.sm_parentId = sm_parentId;
            Model.sm_sort = Number("0" + MaxSort) + 1;
            // Model.OS_CreateTime = moment().format("YYYY-MM-DD HH:mm:ss");
        }

        Model.sm_name = data.sm_name;
        Model.sm_showAddr = data.sm_showAddr;
        Model.sm_icon=data.sm_icon;
        Model.sm_url = data.sm_url;
        Model.sm_blank =data.sm_blank ? 1 : 0;
        Model.sm_attr=data.sm_attr;
        if (sm_id && sm_id!=='') {
            await this.DB.cms_site_menu.Access.update(Model, {
                where: {
                    sm_id: sm_id
                }
            });
        } else {
            await this.DB.cms_site_menu.Access.create(Model);
        }
        await this.CheckIsParent(Model.zd_ID);
        return Model;
    }

    /**
     * 删除
     * @param {*} CurNodeParentID 当前节点的父级ID
     * @param {*} AllIDs_Array 待删除的节点ID数组
     */
    async delModule(CurNodeParentID,AllIDs_Array){
        for (var i = 0; i < AllIDs_Array.length; i++) {
            let id = Number("0" + AllIDs_Array[i]);
            if (id > 0) {
                await this.DB.cms_site_menu.Access.destroy({
                    where: {
                        sm_id: id
                    }
                });
            }
        }
        await this.CheckIsParent(CurNodeParentID);
        return true;
    }

    async moveNode(queryModel){
        if (queryModel.CurNodeID !=="") {
            await this.DB.cms_site_menu.Access.update({
                sm_parentId: queryModel.TargetParentID
            }, {
                where: {
                    sm_id: queryModel.CurNodeID
                }
            });
        }
        for (let i = 0; i < queryModel.TargetNodeChildrensIDs_array.length; i++) {
            let id = queryModel.TargetNodeChildrensIDs_array[i];
            await this.DB.cms_site_menu.Access.update({
                sm_sort: i
            }, {
                where: {
                    sm_id: id
                }
            });
        }
        await this.CheckIsParent(queryModel.TargetParentID);
        await this.CheckIsParent(queryModel.old_parent);
        return true;

    }

    async asycDict(){
       let ret = await this.func.cmsSiteMenu.Sync();
       return ret;
    }

    async getTreeData(queryModel){

        let treeData = await this.func.iframeDict.GetTreeData(queryModel.label, queryModel.parentId);
        return treeData;
    }

    /**
     *
     * 检测是否是父级
     * @param {*} ParentID
     */
    async CheckIsParent(ParentID){
        if (!isNaN(ParentID)  || ParentID === "") {
            return false;
        }
        let count = await this.DB.cms_site_menu.Access.count({
            where: {
                sm_parentId: ParentID
            }

        });
        if (count === 0) {
            await this.DB.cms_site_menu.Access.update({
                sm_isParent: 0
            }, {
                where: {
                    sm_id: ParentID
                }
            });
        } else {
            await this.DB.cms_site_menu.Access.update({
                sm_isParent: 1
            }, {
                where: {
                    sm_id: ParentID
                }
            });
        }
        return true;

    }
};
